

import base from "./baseUrl.js"
let base_url = base.baseUrl
 // http://wechat.lanhi.com.cn:5010/
 export const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数 
 	// 判断请求类型
 	let headerData = {
 		'content-type': 'application/json'
 	}
 	//如果存在content-type就用 没有则默认
 	if (prams.headers) {
 		headerData = {
 			...prams.headers
 		}
 	}
 	// 获取token
 	let token = uni.getStorageSync('access_token')
 	let dataOrgId = uni.getStorageSync('dataOrgId') || ''
 	let dataObj = {
 		...prams.query,
 	}
	if(dataOrgId && !dataObj.dataOrgId){
		dataObj.dataOrgId=dataOrgId
	}
 	if (token) {
 		headerData['Authorization'] = token
 	}
	if (prams.url == '/mobile/login/loginByPassword') {
		headerData['Authorization'] = ''
	}
 	return new Promise((resolve, reject) => {
 		let url = base_url + prams.url;
 		if (headerData.showLoding) {
 			uni.showLoading({
 				title: "加载中",
 				mask: true,
 			});
 		}
 		return uni.request({
 			url: url,
 			data: dataObj,
 			method: prams.method,
 			header: headerData,
 			success: (res) => {
 				uni.hideLoading()
 				//console.log(res.data)
 				if (res.data.code == 401) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
 					return;
 				} 
				if (res.data.status != 200 && res.data.message) {
 					uni.showToast({
 						title: res.data.message,
 						duration: 2000,
 						icon: "none"
 					})
 				}
				if(res.data.code !== 200 && res.data.msg){
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: "none"
					})
				}
 				resolve(res.data);

 			},
 			fail: (err) => {
 				//console.log('err:', err);
 				reject(err);
 				uni.hideLoading()
 			},
 			complete: () => {
 				//console.log('请求完成')
 				uni.hideLoading()
 			}
 		});
 	})
 }